package cn.net.ecode.elec.api.service;

import cn.net.ecode.common.service.CrudService;
import cn.net.ecode.common.utils.IdGen;
import cn.net.ecode.common.utils.StringUtils;
import cn.net.ecode.constant.AdminTypeEnum;
import cn.net.ecode.constant.YesOrNoEnum;
import cn.net.ecode.elec.api.entity.SubAdminForm;
import cn.net.ecode.exception.TPException;
import cn.net.ecode.mdm.entity.MdmBtypeInfo;
import cn.net.ecode.mdm.service.MdmBtypeInfoService;
import cn.net.ecode.modules.sys.dao.EmployeeDao;
import cn.net.ecode.modules.sys.dao.UserDao;
import cn.net.ecode.modules.sys.entity.Employee;
import cn.net.ecode.modules.sys.entity.Office;
import cn.net.ecode.modules.sys.entity.Role;
import cn.net.ecode.modules.sys.entity.User;
import cn.net.ecode.modules.sys.service.EmployeeService;
import cn.net.ecode.modules.sys.service.OfficeService;
import cn.net.ecode.modules.sys.service.RoleService;
import cn.net.ecode.modules.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserService
 * @Description 用户扩展服务
 * @Author AlanShelby
 * @Date 2019/8/27 0027 10:05
 * @Version 1.0
 */
@Service
@Transactional(readOnly = true)
public class ElecUserService extends CrudService<UserDao, User> {

    @Autowired
    private UserService userService;

    @Autowired
    private EmployeeDao employeeDao;

    @Autowired
    private MdmBtypeInfoService mdmBtypeInfoService;

    @Autowired
    private OfficeService officeService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserDao userDao;

    /**
     * @Method registerAdmin
     * @Description 注册管理员
     * @Author AlanShelby
     * @Date 10:06 2019/8/27 0027
     * @Param [elecAd]
     * @Return void
     */
    @Transactional(readOnly = false)
    public void registerAdmin(MdmBtypeInfo mdmBtypeInfo) {
        if (!StringUtils.equals(mdmBtypeInfo.getPassword(), mdmBtypeInfo.getValidatePassword())) {
            throw new TPException("两次输入的密码不匹配，请确认。");
        }

        // 保存企业信息
        mdmBtypeInfo.setIsNewRecord(true);
        mdmBtypeInfo.setId(IdGen.nextId());
        mdmBtypeInfoService.save(mdmBtypeInfo);

        String loginCode = mdmBtypeInfo.getLinkmanTel();
        String userName = mdmBtypeInfo.getLinkman();
        User currentUser = new User("system");

        // 保存员工信息
        Office office = officeService.get("qiancheng");
        if (office == null) {
            throw new TPException("数据异常，请联系管理员");
        }
        Employee employee = new Employee();
        employee.setIsNewRecord(true);
        employee.setEmpCode(loginCode);
        employee.setEmpName(userName);
        employee.setEmpNo(loginCode);
        employee.setSex("1");
        employee.setMobile(loginCode);
        employee.getOffice().setOfficeCode(office.getOfficeCode());
        employee.getCompany().setCompanyCode(office.getOfficeCode());
        employee.getCompany().setCompanyName(office.getOfficeName());
        employee.setCreateBy(currentUser);
        employee.setUpdateBy(currentUser);
        employeeDao.insert(employee);

        // 保存用户信息
        User user = new User();
        List<Role> roleList = new ArrayList<>();
        Role role = roleService.get("admin");
        if (role == null) {
            throw new TPException("角色数据异常，请联系管理员");
        }
        roleList.add(role);

        user.setIsNewRecord(true);
        user.setUserCode(loginCode);
        user.setUserName(userName);
        user.setMobile(loginCode);
        user.setRefCode(loginCode);
        user.setRefName(userName);
        user.setStatus("0");
        user.setCreateBy(currentUser);
        user.setUpdateBy(currentUser);
        user.setLoginCode(loginCode);
        user.setCorpCode(office.getOfficeCode());
        user.setCorpName(office.getOfficeName());
        user.setExtendS1(mdmBtypeInfo.getId());
        user.setExtendS2(mdmBtypeInfo.getBtypeName());
        user.setExtendS3(String.valueOf(YesOrNoEnum.NO.getCode()));
        user.setExtendS4(String.valueOf(AdminTypeEnum.ADMIN.getCode()));
        user.setExtendS5(mdmBtypeInfo.getPost());
        user.setRemarks(mdmBtypeInfo.getDeptName());
        user.setRoleList(roleList);
        try {
            userService.addUser(user);
        } catch (Exception e) {
            throw new TPException("注册异常，请联系管理员");
        }

        userService.updatePassword(user.getLoginCode(), mdmBtypeInfo.getPassword());
    }

    @Transactional(readOnly = false)
    public void registerAdminNoMdm(MdmBtypeInfo mdmBtypeInfo) {
        String loginCode = mdmBtypeInfo.getLinkmanTel();
        String userName = mdmBtypeInfo.getLinkman();
        User currentUser = new User("system");

        // 保存员工信息
        Office office = officeService.get("qiancheng");
        if (office == null) {
            throw new TPException("数据异常，请联系管理员");
        }
        Employee employee = new Employee();
        employee.setIsNewRecord(true);
        employee.setEmpCode(loginCode);
        employee.setEmpName(userName);
        employee.setEmpNo(loginCode);
        employee.setSex("1");
        employee.setMobile(loginCode);
        employee.getOffice().setOfficeCode(office.getOfficeCode());
        employee.getCompany().setCompanyCode(office.getOfficeCode());
        employee.getCompany().setCompanyName(office.getOfficeName());
        employee.setCreateBy(currentUser);
        employee.setUpdateBy(currentUser);
        employeeDao.insert(employee);

        // 保存用户信息
        User user = new User();
        List<Role> roleList = new ArrayList<>();
        Role role = roleService.get("admin");
        if (role == null) {
            throw new TPException("角色数据异常，请联系管理员");
        }
        roleList.add(role);

        user.setIsNewRecord(true);
        user.setUserCode(loginCode);
        user.setUserName(userName);
        user.setMobile(loginCode);
        user.setRefCode(loginCode);
        user.setRefName(userName);
        user.setStatus("0");
        user.setCreateBy(currentUser);
        user.setUpdateBy(currentUser);
        user.setLoginCode(loginCode);
        user.setCorpCode(office.getOfficeCode());
        user.setCorpName(office.getOfficeName());
        user.setExtendS1(mdmBtypeInfo.getId());
        user.setExtendS2(mdmBtypeInfo.getBtypeName());
        user.setExtendS3(String.valueOf(YesOrNoEnum.NO.getCode()));
        user.setExtendS4(String.valueOf(AdminTypeEnum.ADMIN.getCode()));
        user.setExtendS5("管理员");
        user.setRoleList(roleList);
        userService.addUser(user);
    }

    /**
     * @Method registerSubAdmin
     * @Description 注册子管理员
     * @Author AlanShelby
     * @Date 15:36 2019/8/27 0027
     * @Param [subAdminForm]
     * @Return void
     */
    @Transactional(readOnly = false)
    public void registerSubAdmin(SubAdminForm subAdminForm) {
        User subAdmin = userService.get(subAdminForm.getPhoneNumber());
        if (subAdmin != null) {
            throw new TPException("该账号已存在，无法进行分配。");
        }

        // 查询企业信息
        MdmBtypeInfo params = new MdmBtypeInfo();
        params.setId(subAdminForm.getBtypeCode());
        MdmBtypeInfo btypeInfo = mdmBtypeInfoService.get(params);
        if (btypeInfo == null) {
            throw new TPException("企业信息异常，请联系管理员");
        }
        User currentUser = new User("system");

        // 保存员工信息
        Office office = officeService.get("qiancheng");
        if (office == null) {
            throw new TPException("数据异常，请联系管理员");
        }
        Employee employee = new Employee();
        employee.setIsNewRecord(true);
        employee.setEmpCode(subAdminForm.getPhoneNumber());
        employee.setEmpName(subAdminForm.getUserName());
        employee.setEmpNo(subAdminForm.getPhoneNumber());
        employee.setSex("1");
        employee.setMobile(subAdminForm.getPhoneNumber());
        employee.getOffice().setOfficeCode(office.getOfficeCode());
        employee.getCompany().setCompanyCode(office.getOfficeCode());
        employee.getCompany().setCompanyName(office.getOfficeName());
        employee.setCreateBy(currentUser);
        employee.setUpdateBy(currentUser);
        employeeDao.insert(employee);

        // 保存用户信息
        User user = new User();
        List<Role> roleList = new ArrayList<>();
        Role role = roleService.get("subadmin");
        if (role == null) {
            throw new TPException("角色数据异常，请联系管理员");
        }
        roleList.add(role);

        user.setIsNewRecord(true);
        user.setUserCode(subAdminForm.getPhoneNumber());
        user.setUserName(subAdminForm.getUserName());
        user.setMobile(subAdminForm.getPhoneNumber());
        user.setRefCode(subAdminForm.getPhoneNumber());
        user.setRefName(subAdminForm.getUserName());
        user.setStatus("0");
        user.setCreateBy(currentUser);
        user.setUpdateBy(currentUser);
        user.setLoginCode(subAdminForm.getPhoneNumber());
        user.setCorpCode(office.getOfficeCode());
        user.setCorpName(office.getOfficeName());
        user.setExtendS1(btypeInfo.getId());
        user.setExtendS2(btypeInfo.getBtypeName());
        user.setExtendS3(String.valueOf(YesOrNoEnum.YES.getCode()));
        user.setExtendS4(String.valueOf(AdminTypeEnum.SUB_ADMIN.getCode()));
        user.setExtendS5(subAdminForm.getExtendS5());
        user.setRoleList(roleList);
        userService.addUser(user);
    }

    /**
     * @Method findSubAdminList
     * @Description 查询子管理员列表
     * @Author AlanShelby
     * @Date 16:03 2019/9/8 0008
     * @Param [user]
     * @Return java.util.List<cn.net.ecode.modules.sys.entity.User>
     */
    public List<User> findSubAdminList(User user) {
        return userDao.findSubAdminList(user);
    }
}